// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

package main

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"strconv"
)

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

// index is private function  to define
// the web server handler for "/" route (known as index)
//
// it reads from environmental storage name of
// the web server owner and returns to  client
// the string like:
// "Hello, <client_name>! You are on <owner_name>'s web server!"
//
// <owner_name>: reads as the "GET" request param
// valid client url example: localhost:<port>/?name="aleksei"
func index(w http.ResponseWriter, r *http.Request) {

	var o string = os.Getenv("OWNER")
	var c string = r.URL.Query().Get("name")

	w.Write([]byte(fmt.Sprintf("Hello, %s! You are on %s's web server!", c, o)))

}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

// main  is  private function
// to start program main loop
func main() {

	p, pErr := strconv.ParseInt(os.Getenv("PORT"), 10, 64)
	if pErr != nil {
		log.Fatal("web server port is not defined ", pErr)
	}

	http.HandleFunc("/", index)
	http.ListenAndServe(fmt.Sprintf(":%d", p), nil)

}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
